package org.one.stone.soup.grfxML;

import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.xml.XmlElement;
  /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public class DataImage extends DataGeometry implements ITag {
	public static String DEFAULT_ID = "_Image";

		DataString file = new DataString(TagModifier.STRING_TYPE_FILE);
		DataPoint size = new DataPoint();
		DataPoint sizeView = new DataPoint();
		DataState rotatable = new DataState(false);
//		DataScale transparency = new DataScale(1);

		GeometryImage _image = null;
		DataTranslation _translation;
public DataImage() {
	try{
		setId(DEFAULT_ID,this);
	}
	catch(LockException le){} //Dummy catch
}
public void buildFromgrfxML(XmlElement xml)
{
	String name = xml.getAttributeValueByName(Token.ID);
	if(name!=null)
	{
		try{
			setId(name,this);
		}
		catch(LockException le){} //Dummy catch
	}

	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.FILE))
		{
			file = new DataString(TagModifier.STRING_TYPE_FILE);
	 		file.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.SIZE))
		{
			size = new DataPoint(TagModifier.POINT_TYPE_BOUNDS);
			size.buildFromgrfxML(element,TagModifier.POINT_TYPE_BOUNDS);
		}
		else if(eleName.equals(Token.ROTATABLE))
		{
			rotatable = new DataState(false);
			rotatable.buildFromgrfxML(element);
		}
/*		else if(eleName.equals(Token.TRANSPARENCY))
		{
			transparency = new DataScale(1);
	 		transparency.buildFromgrfxML(element);
		}*/
		else if(super.buildFromElement(element)==false && !NameHelper.isSystemName(element.getName()))
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.IMAGE,""+element.getStartLineNo()} );
		}
	}
}
public void copyFromgrfxML(XmlElement xml)
{

	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.FILE))
		{
	 		file.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.SIZE))
		{
			size.copyFromgrfxML(element,TagModifier.POINT_TYPE_BOUNDS);
		}
		else if(eleName.equals(Token.ROTATABLE))
		{
			rotatable.copyFromgrfxML(element);
		}
/*		else if(eleName.equals(Token.TRANSPARENCY))
		{
	 		transparency.copyFromgrfxML(element);
		}*/
		else if(super.copyFromElement(element)==false)
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.IMAGE,""+element.getStartLineNo()} );
		}
	}
}
public DataGeometry getClone() {

	DataImage newImage = new DataImage();

	super.getClonedElements(newImage);

	newImage.file = file.getClone();
	newImage.rotatable = rotatable.getClone();
	newImage.size = size.getClone();
	//newImage.transparency = transparency.getClone();

	return newImage;
}
public DataGeometry getClone(String path) {

	DataImage newImage = new DataImage();

	GrfxMLStoreProcessor.setCloneName( path,this,newImage );

	super.getClonedElements(path,newImage);

	newImage.file = file.getClone(path);
	newImage.rotatable = rotatable.getClone(path);
	newImage.size = size.getClone(path);
//	newImage.transparency = transparency.getClone(path);

	return newImage;
}
public DataString getFile() {
	return file;
}
public Geometry getGeometry(GrfxMLEngine engine)
{
	if (_image == null && _translation!=null) {
		_image = new GeometryImage(engine,_translation.window,bounds,_translation.visibleView, color, filled, transparency, rotatable, _translation.rotationView, _translation.positionView, offsetView, file, size, sizeView);
		try {
			GrfxMLProcessor.translate(size, _translation, sizeView);
		} catch (LockException le) {
			le.printStackTrace();
			if( !NameHelper.isSystemName(getId()) )
			{
				_image.setId(getId());
			}
		}
	}

	return(_image);
}
public DataState getRotatable() {
	return rotatable;
}
public DataPoint getSize() {
	return size;
}
public DataTranslation getTranslation() {
	return _translation;
}
/*public DataScale getTransparency() {
	return transparency;
}*/
public void register(DataLinker store) {

	super.register(store);

	try{
		store.add(file,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(size,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(rotatable,this);
	}
	catch(LockException le)
	{}//Dummy catch

/*	try{
		store.add(transparency,this);
	}
	catch(LockException le)
	{}//Dummy catch*/
}
public void replace(Field oldObj, Field newObj) {
	replace(oldObj, newObj, false);
}
public void replace(Field oldObj, Field newObj,boolean recursive) {

	_image=null;

	super.replace(oldObj,newObj);

	if(file==oldObj)
		file=(DataString)newObj;

	if(size==oldObj)
		size=(DataPoint)newObj;

	if(rotatable==oldObj)
		rotatable=(DataState)newObj;

/*	if(transparency==oldObj)
		transparency=(DataScale)newObj;*/
}
public void resetChanged() {
	super.resetChanged();
	file.resetChanged();
	size.resetChanged();
	sizeView.resetChanged();
	rotatable.resetChanged();
	//transparency.resetChanged();
}
public void setFile(DataString newFile) {
	file = newFile;
}
public void setImage(GeometryImage newImage) {
	_image = newImage;
}
public void setRotatable(DataState newRotatable) {
	rotatable = newRotatable;
}
public void setSize(DataPoint newSize) {
	size = newSize;
}
protected void setTranslation(DataTranslation newTranslation) {
	_translation = newTranslation;
}
/*protected void setTransparency(DataScale newTransparency) {
	transparency = newTransparency;
}*/
public String togrfxML(String name) {
	String xml = "<" + name;

	if(!NameHelper.isSystemName(getId()))
	{
		xml += " id=\""+getId()+"\"";
	}

	xml += ">";
	xml += super.togrfxML(name);
	xml += file.togrfxML(Token.FILE);
	xml += size.togrfxML(Token.SIZE,TagModifier.POINT_TYPE_BOUNDS);
	xml += rotatable.togrfxML(Token.ROTATABLE);
//	xml += transparency.togrfxML(Token.TRANSPARENCY);
	xml += "</" + name + ">\n";
	return xml;
}
public String toString()
{
	if(GrfxMLEngine.DEBUG)
		return(togrfxML( DEFAULT_ID.substring(1) ));

	String xml = "<"+DEFAULT_ID.substring(1)+"/>";

	return xml;
}
public void translate(DataTranslation t) {

	try {
		GrfxMLProcessor.translate(size, TagModifier.POINT_TYPE_BOUNDS, t, sizeView);
	} catch (LockException le) {
		le.printStackTrace();
	}

	super.translate(t);

	_translation = t;

}
}
